package com.ruoyi.smart.service.impl;

import java.text.DecimalFormat;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.smart.domain.SmartPlan;
import com.ruoyi.smart.domain.SmartProduct;
import com.ruoyi.smart.domain.SmartProductModel;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.smart.mapper.SmartPlanTaskMapper;
import com.ruoyi.smart.domain.SmartPlanTask;
import com.ruoyi.smart.service.ISmartPlanTaskService;

/**
 * 计划任务Service业务层处理
 * 
 * @author minwei
 * @date 2022-04-26
 */
@Service
public class SmartPlanTaskServiceImpl implements ISmartPlanTaskService 
{
    @Autowired
    private SmartPlanTaskMapper smartPlanTaskMapper;

    @Autowired
    private SmartProductModelServiceImpl smartProductModelService;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询计划任务
     * 
     * @param id 计划任务主键
     * @return 计划任务
     */
    @Override
    public SmartPlanTask selectSmartPlanTaskById(Long id)
    {
        SmartPlanTask smartPlanTask=smartPlanTaskMapper.selectSmartPlanTaskById(id);
        insertMSG(smartPlanTask);
        return smartPlanTask;
    }

    /**
     * 查询计划任务列表
     * 
     * @param smartPlanTask 计划任务
     * @return 计划任务
     */
    @Override
    public List<SmartPlanTask> selectSmartPlanTaskList(SmartPlanTask smartPlanTask)
    {
        List<SmartPlanTask> list=smartPlanTaskMapper.selectSmartPlanTaskList(smartPlanTask);
        for (SmartPlanTask item : list){
            insertMSG(item);
        }
        return list;
    }

    //插入用户，组织，产品等信息
    private SmartPlanTask insertMSG(SmartPlanTask smartPlanTask){
        smartPlanTask.setProductModel(smartProductModelService.selectSmartProductModelById(smartPlanTask.getProductId()));
        smartPlanTask.setOrganizeorganaz(deptService.selectDeptById(smartPlanTask.getOrganizeorganazId()));
        smartPlanTask.setDoUser(userService.selectUserById(smartPlanTask.getDoUserId()));
        smartPlanTask.setCreateUser(userService.selectUserById(smartPlanTask.getCreateUserId()));
        smartPlanTask.setSchedule(getSchedule(smartPlanTask));
        return smartPlanTask;
    }

    //获取进度
    private String getSchedule(SmartPlanTask smartPlanTask){
        if(smartPlanTask.getNum()!=null&&smartPlanTask.getPlanNum()!=null)
        {
            return String.format("%.2f",Double.valueOf(smartPlanTask.getNum()/smartPlanTask.getPlanNum()));
        } else {
            return "0";
        }
    }

    /**
     * 新增计划任务
     * 
     * @param smartPlanTask 计划任务
     * @return 结果
     */
    @Override
    public int insertSmartPlanTask(SmartPlanTask smartPlanTask)
    {
        return smartPlanTaskMapper.insertSmartPlanTask(smartPlanTask);
    }

    /**
     * 修改计划任务
     * 
     * @param smartPlanTask 计划任务
     * @return 结果
     */
    @Override
    public int updateSmartPlanTask(SmartPlanTask smartPlanTask)
    {
        return smartPlanTaskMapper.updateSmartPlanTask(smartPlanTask);
    }

    /**
     * 批量删除计划任务
     * 
     * @param ids 需要删除的计划任务主键
     * @return 结果
     */
    @Override
    public int deleteSmartPlanTaskByIds(Long[] ids)
    {
        return smartPlanTaskMapper.deleteSmartPlanTaskByIds(ids);
    }

    /**
     * 删除计划任务信息
     * 
     * @param id 计划任务主键
     * @return 结果
     */
    @Override
    public int deleteSmartPlanTaskById(Long id)
    {
        return smartPlanTaskMapper.deleteSmartPlanTaskById(id);
    }
    /**
     * 添加产品查询计划方法
     */
    @Override
    public SmartPlanTask getPlanByProductCode(String code){
        SmartPlanTask smartPlan =new SmartPlanTask();
        //以下是判断方式
        String code_=code.substring(0,4);
        smartPlan.setCode(code_);
        List<SmartPlanTask> list=smartPlanTaskMapper.selectSmartPlanTaskList(smartPlan);
        if(list.size()>0){
            for(SmartPlanTask item : list){
                //判断产品是否属于计划
                if(code_.equals(item.getCode().substring(0,4))){
                    smartPlan=item;
                }
            }
        }
        return smartPlan;
    }

    /**
     * 添加任务计划数量
     * @param id  计划id
     * @param quality   添加数量
     */
    public void addPlanQuality(Long id,Integer quality){
        SmartPlanTask smartPlan=smartPlanTaskMapper.selectSmartPlanTaskById(id);
        if(smartPlan.getNum()==null) smartPlan.setNum(0l);
        smartPlan.setNum(smartPlan.getNum()+quality);
        smartPlanTaskMapper.updateSmartPlanTask(smartPlan);
    }

}
